﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using Ezio.Model;

namespace Ezio.Service
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [XmlSerializerFormat]
    public class AuthenticationService : BaseService
    {
        [WebInvoke(UriTemplate = "", Method = "POST")]
        public LoginResult Login(string Username, string Password)
        {
            LoginResult LoginResult = new LoginResult();
            LoginResult.Result = Provider.IsValid(Username, Password).First().Value;

            if (LoginResult.Result)
            {
                string Token = Guid.NewGuid().ToString();

                Provider.AddToSessions(new Sessions
                {
                    ExpiryDate = DateTime.Now.AddMinutes(10),
                    Token = Token,
                    Username = Username
                });

                LoginResult.Token = Token;
                LoginResult.Result = Provider.TrySaveChanges();
            }

            return LoginResult;
        }
    }
}